import { _decorator, Animation, AudioSource, Button, Component, EventHandler, ImageAsset, Node, Sprite, spriteAssembler, tween, Vec3 } from 'cc';
import ads from '../PlayableFramework/Ads/AdsManager';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {


    @property(AudioSource)
    public audio1:AudioSource;
    @property(AudioSource)
    public audio2:AudioSource;

    @property(Animation)
    public switchEffect:Animation;

    @property(Animation)
    public cEffect:Animation;


    @property(Node)
    public hair1Parent : Node
    
    @property(Node)
    public hair2Parent : Node

    @property(Node)
    public clothParent : Node

    @property(Node)
    public shoesParent : Node

    @property(Node)
    public necklaceParent : Node

    public level :number;

    @property(Node)
    public levelNodes:Node[] = [];


    @property(Node)
    public guide : Node

    @property(Node)
    public nextBtn : Node

    private isClick:boolean = false;

    protected onLoad(): void {
        ads.init();
    }

    protected update(dt: number): void {
        this.nextBtn.active = this.isClick;
    }

    
    start() {
        tween(this.guide).delay(5).call(()=>{
            this.guide.active = false;
        }).start();
        
        this.level = -1;
        this.next();
        for (let j = 0; j < this.levelNodes.length; j++) {
            for (let i = 0; i < this.levelNodes[j].children.length; i++) {
                const index = i + 1;
                const element = this.levelNodes[j].children[i];
                const btn = element.getComponent(Button);

                btn.node.children[1].active = false;

                const e =  new EventHandler();
                e.target = this.node;
                e.component = "Main";
                switch (j) {
                    case 0:
                        e.handler = "clickClothBtn"
                        break;
                    case 1:
                        e.handler = "clickHairBtn"
                        break;
                    case 2:
                        e.handler = "clickShoesBtn"
                        break;
                    case 3:
                        e.handler = "clickNecklaceBtn"
                        break;
                    default:
                        break;
                }
                e.customEventData = index.toString();
                btn.clickEvents.push(e);
            }
        }

        for (let i = 0; i < this.hair1Parent.children.length; i++) {
            const element1 = this.hair1Parent.children[i];
            const element2 = this.hair2Parent.children[i];
            element1.active = element1.name == "0";
            element2.active = element2.name == "0";
        }

        for (let i = 0; i < this.clothParent.children.length; i++) {
            const element = this.clothParent.children[i];
            element.active = element.name == "0";
        }

        for (let i = 0; i < this.necklaceParent.children.length; i++) {
            const element = this.necklaceParent.children[i];
            element.active = element.name == "0";
        }

        
        for (let i = 0; i < this.shoesParent.children.length; i++) {
            const element = this.shoesParent.children[i];
            element.active = element.name == "0";
        }
    }

    next(){
        this.level++;
        for (let i = 0; i < this.levelNodes.length; i++) {
            const element = this.levelNodes[i];
            element.active = this.level == i;
            if(i!=0&&this.level == i){
                element.position = new Vec3(0,-500,0);
                tween(element).to(0.5,{position:new Vec3(0,0,0)}).start();
            }
        }
        this.clickBtn(-1);
        if(this.level == 3){
            this.isClick = false;
        }

    }


    clickHairBtn(event:Event,data:string){
        console.log(data);
        const index = Number.parseInt(data);
        this.clickBtn(index - 1);
        this.clickEffect(index - 1);
        for (let i = 0; i < this.hair1Parent.children.length; i++) {
            const element1 = this.hair1Parent.children[i];
            const element2 = this.hair2Parent.children[i];
            element1.active = element1.name == data;
            element2.active = element2.name == data;
        }
    }

    clickShoesBtn(event:Event,data:string){
        console.log(data);
        const index = Number.parseInt(data);
        this.clickBtn(index - 1);
        this.clickEffect(index - 1);
        for (let i = 0; i < this.shoesParent.children.length; i++) {
            const element = this.shoesParent.children[i];
            element.active = element.name == data;
        }
    }

    clickClothBtn(event:Event,data:string){
        console.log(data);
        const index = Number.parseInt(data);
        this.clickBtn(index - 1);
        this.clickEffect(index - 1);

        for (let i = 0; i < this.clothParent.children.length; i++) {
            const element = this.clothParent.children[i];
            element.active = element.name == data;
        }
    }

    clickNecklaceBtn(event:Event,data:string){
        console.log(data);
        // const index = Number.parseInt(data);
        // this.clickBtn(index - 1);
        // this.clickEffect();
        // for (let i = 0; i < this.necklaceParent.children.length; i++) {
        //     const element = this.necklaceParent.children[i];
        //     element.active = element.name == data;
        // }

        ads.redirect();
        ads.end();
    }

    clickBtn(index : number){
        for (let j = 0; j < this.levelNodes.length; j++) {
            for (let i = 0; i < this.levelNodes[j].children.length; i++) {
                const element = this.levelNodes[j].children[i];
                const btn = element.getComponent(Button);
                btn.node.children[1].active = i == index;
            }
        }
    }

    clickEffect(index:number){
        this.audio1.playOneShot(this.audio1.clip);
        this.audio2.playOneShot(this.audio2.clip);
        this.switchEffect.play();
        this.isClick = true;
        this.guide.active = false;
        this.cEffect.play();
        this.cEffect.node.worldPosition = this.levelNodes[this.level].children[index].worldPosition;
    }
}


